#!/bin/bash

# 环境检查与依赖安装脚本
# 支持CentOS 7.2+ 和 Ubuntu 16.04+
# 作者：Atanycosts
# B站作者链接：https//:space.bilibili.com/499719031?spm_id_from=333.1007.0.0
# 作者简介：Atanycosts是一名热爱技术的开发者，专注于开源项目和技术分享。
# 联系方式：atanycosts@example.com

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # 无颜色


# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
  echo -e "${RED}请使用root用户运行此脚本！${NC}"
  exit 1
fi

# 检查操作系统类型
OS_TYPE=""
if [ -f /etc/centos-release ]; then
  OS_TYPE="centos"
elif [ -f /etc/lsb-release ]; then
  OS_TYPE="ubuntu"
else
  echo -e "${RED}不支持的操作系统！请使用CentOS 7.2+或Ubuntu 16.04+。${NC}"
  exit 1
fi

echo -e "${GREEN}检测到操作系统：$OS_TYPE${NC}"

# 安装基础依赖
echo -e "${BLUE}正在安装基础依赖（openssl, curl, wget, git, nginx, dos2unix）...${NC}"
if [ "$OS_TYPE" == "centos" ]; then
  yum update -y
  yum install -y openssl curl wget git nginx dos2unix
elif [ "$OS_TYPE" == "ubuntu" ]; then
  apt-get update -y
  apt-get install -y openssl curl wget git nginx dos2unix
fi

# 检查Java
echo -e "${BLUE}正在检查Java环境...${NC}"
if ! command -v java &> /dev/null; then
  echo -e "${YELLOW}Java未安装，正在安装JDK 8u191...${NC}"
  if [ "$OS_TYPE" == "centos" ]; then
    wget http://atanycosts.cn/file/path/jdk-8u191.tar.gz -O /tmp/jdk-8u191.tar.gz
    tar -zxvf /tmp/jdk-8u191.tar.gz -C /usr/local/
    echo "export JAVA_HOME=/usr/local/jdk1.8.0_191" >> /etc/profile
    echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
    source /etc/profile
  elif [ "$OS_TYPE" == "ubuntu" ]; then
    wget http://atanycosts.cn/file/path/jdk-8u191.tar.gz -O /tmp/jdk-8u191.tar.gz
    tar -zxvf /tmp/jdk-8u191.tar.gz -C /usr/local/
    echo "export JAVA_HOME=/usr/local/jdk1.8.0_191" >> /etc/profile
    echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
    source /etc/profile
  fi
else
  echo -e "${GREEN}Java已安装，版本信息：${NC}"
  java -version
fi

# 检查MySQL
echo -e "${BLUE}正在检查MySQL环境...${NC}"
if ! command -v mysql &> /dev/null; then
  echo -e "${YELLOW}MySQL未安装，正在安装MySQL 5.7...${NC}"
  if [ "$OS_TYPE" == "centos" ]; then
    wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    rpm -ivh mysql57-community-release-el7-11.noarch.rpm
    yum install -y mysql-community-server
    systemctl start mysqld
    systemctl enable mysqld
  elif [ "$OS_TYPE" == "ubuntu" ]; then
    apt-get install -y mysql-server
    systemctl start mysql
    systemctl enable mysql
  fi
else
  echo -e "${GREEN}MySQL已安装，版本信息：${NC}"
  mysql --version
fi

# 检查Python
echo -e "${BLUE}正在检查Python环境...${NC}"
if ! command -v python3 &> /dev/null; then
  echo -e "${YELLOW}Python 3未安装，正在安装Python 3.6+...${NC}"
  if [ "$OS_TYPE" == "centos" ]; then
    yum install -y python36
  elif [ "$OS_TYPE" == "ubuntu" ]; then
    apt-get install -y python3
  fi
else
  echo -e "${GREEN}Python 3已安装，版本信息：${NC}"
  python3 --version
fi

# 安装PyMySQL
echo -e "${BLUE}正在安装PyMySQL...${NC}"
if [ "$OS_TYPE" == "centos" ]; then
  yum install -y python36-pip
  pip3 install PyMySQL
elif [ "$OS_TYPE" == "ubuntu" ]; then
  apt-get install -y python3-pip
  pip3 install PyMySQL
fi

# 检查网络策略
echo -e "${YELLOW}请确保以下端口已开放：${NC}"
echo -e "1. WeBASE管理平台端口：${GREEN}5000${NC}"
echo -e "2. 节点前置端口：${GREEN}5002${NC}"
echo -e "${YELLOW}如果使用云服务器，请在安全组中开放上述端口。${NC}"

# 输出结束信息
echo -e "${GREEN}环境检查与依赖安装完成！${NC}"
echo -e "${MAGENTA}感谢您使用此脚本！${NC}"
echo -e "${CYAN}希望您在开发中一切顺利！${NC}"

# ASCII艺术
echo -e "${GREEN}"
cat << "EOF"
  ______     __                                                                    __
 /      \   /  |                                                                  /  |
/$$$$$$  | _$$ |_     ______   _______   __    __   _______   ______    _______  _$$ |_    _______
$$ |__$$ |/ $$   |   /      \ /       \ /  |  /  | /       | /      \  /       |/ $$   |  /       |
$$    $$ |$$$$$$/    $$$$$$  |$$$$$$$  |$$ |  $$ |/$$$$$$$/ /$$$$$$  |/$$$$$$$/ $$$$$$/  /$$$$$$$/
$$$$$$$$ |  $$ | __  /    $$ |$$ |  $$ |$$ |  $$ |$$ |      $$ |  $$ |$$      \   $$ | __$$      \
$$ |  $$ |  $$ |/  |/$$$$$$$ |$$ |  $$ |$$ \__$$ |$$ \_____ $$ \__$$ | $$$$$$  |  $$ |/  |$$$$$$  |
$$ |  $$ |  $$  $$/ $$    $$ |$$ |  $$ |$$    $$ |$$       |$$    $$/ /     $$/   $$  $$//     $$/
$$/   $$/    $$$$/   $$$$$$$/ $$/   $$/  $$$$$$$ | $$$$$$$/  $$$$$$/  $$$$$$$/     $$$$/ $$$$$$$/
                                        /  \__$$ |
                                        $$    $$/
                                         $$$$$$/
EOF
echo -e "${NC}"
# 打印作者信息
echo -e "${BLUE}==============================${NC}"
echo -e "${GREEN}作者信息:${NC}"
echo -e "${YELLOW}姓名：Atanycosts ${NC}"
echo -e "${YELLOW}B站链接：https://space.bilibili.com/499719031?spm_id_from=333.1007.0.0${NC}"
echo -e "${YELLOW}联系方式：atanycosts@example.com${NC}"
echo -e "${BLUE}==============================${NC}"
echo -e "${GREEN}祝您有美好的一天！${NC}"
